package com.xiaobaibai.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xiaobaibai.entity.Coupon;
import com.xiaobaibai.entity.CouponBelong;
import com.xiaobaibai.response.ResponseFormat;
import com.xiaobaibai.service.ICouponBelongService;
import com.xiaobaibai.service.ICouponService;
import com.xiaobaibai.valid.AddGroup;
import com.xiaobaibai.valid.UpdateGroup;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Api(tags = "优惠券管理相关接口")
@RequestMapping("/coupon")
@CrossOrigin
public class CouponController {

    @Autowired
    private ICouponService couponService;

    @Autowired
    private ICouponBelongService couponBelongService;

    @GetMapping("/list")
    @ApiOperation("获取所有优惠券")
    public ResponseFormat getCouponList(){

        QueryWrapper<Coupon> queryWrapper=new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        List<Coupon> list = couponService.list(queryWrapper);

        return ResponseFormat.ok("优惠券列表获取成功",list);
    }

    @PostMapping("/do-coupon")
    @ApiOperation("添加优惠券")
    @CacheEvict(value = "coupon",key = "'getCouponList'")
    public ResponseFormat addCoupon(@RequestBody @Validated(value = AddGroup.class)
                                                Coupon coupon){
        coupon.setCouponReceive(0);
        coupon.setCouponRemaining(coupon.getCouponCount());
        coupon.setCouponStatus(true);
        if(this.myValidCoupon(coupon))
            return ResponseFormat.failure("优惠券信息错误");
        boolean flag = couponService.save(coupon);
        if(!flag)
            return ResponseFormat.failure("增加优惠券失败");
        return ResponseFormat.ok("增加优惠券成功",coupon);
    }

    @PutMapping("/do-coupon")
    @ApiOperation("修改优惠券")
    @CacheEvict(value = "coupon",key = "'getCouponList'")
    public ResponseFormat updateCoupon(@RequestBody @Validated(value = UpdateGroup.class)
                                                   Coupon coupon){
        if(this.myValidCoupon(coupon))
            return ResponseFormat.failure("优惠券信息错误");
        boolean flag = couponService.updateById(coupon);
        if(!flag)
            return ResponseFormat.failure("修改优惠券失败");
        return ResponseFormat.ok("修改优惠券成功",coupon);
    }

    @DeleteMapping("/do-coupon")
    @ApiOperation("删除优惠券")
    @CacheEvict(value = "coupon",key = "'getCouponList'")
    public ResponseFormat deleteCoupon(@RequestParam(value = "id")Integer id){
        boolean flag = couponService.removeById(id);
        if(!flag)
            return ResponseFormat.failure("删除优惠券失败");
        //再把领取表的优惠券也全部删除,不管是否已经使用
        couponBelongService.deleteCouponBelong(id);
        return ResponseFormat.ok("删除优惠券成功");
    }

    @GetMapping("/one/{id}")
    @ApiOperation("获取单个优惠券的信息")
    public ResponseFormat getOneCoupon(@PathVariable(value = "id")Integer id){

        Coupon coupon = couponService.getById(id);

        return ResponseFormat.ok("获取单个优惠券信息成功",coupon);
    }

    @GetMapping("/detail/{id}")
    @ApiOperation("获取单个优惠券的详细信息")
    public ResponseFormat getOneCouponDetail(@PathVariable(value = "id")Integer id){
        Coupon coupon = couponService.getById(id);
        QueryWrapper<CouponBelong> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("coupon_id",id);
        List<CouponBelong> list = couponBelongService.list(queryWrapper);
        Map<String,Object> map=new HashMap<>();
        map.put("coupon",coupon);
        map.put("detail",list);
        return ResponseFormat.ok("获取优惠券详细信息成功",map);
    }

    //优惠券校验逻辑
    private boolean myValidCoupon(Coupon coupon){

        //先看是什么类型
        if(coupon.getCouponType()==0){//满减的
//            BigDecimal bigDecimal = new BigDecimal(coupon.getCouponThreshold());
//            //满减金额要大于门槛,不用
//            if(coupon.getCouponPrice().compareTo(bigDecimal) >= 0){
//                return false;
//            }
            coupon.setCouponDiscount(null);//设置折扣为null
        }
        else if(coupon.getCouponType()==1){
            //0折和10折去掉
            if(coupon.getCouponThreshold()==0||coupon.getCouponThreshold()==10)
                return true;
            //折扣看小数位
            //保留一位小数,去掉多余的
            coupon.setCouponDiscount(coupon.getCouponDiscount().setScale(1,BigDecimal.ROUND_DOWN));
            coupon.setCouponPrice(null);//设置优惠金额为null
        }else{
            return true;
        }
        return false;
    }

}
